<template>
	<view class="page">
		<z-nav-bar></z-nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view class="title">账号绑定</view>
		<view class="input_box"><input type="text" v-model="phone" placeholder="请输入手机号" /></view>
		<!-- <view class="input_box">
			<input type="number" v-model="code" placeholder="请输入验证码" />
			<button @click="getCode">{{codeText}}</button>
		</view> -->
		<view class="input_box"><input v-model="password" placeholder="请输入身份证号码" /></view>
		<view class="input_box"><input type="text" v-model="recommendCode" :disabled="true" placeholder="请选择绑定的地市"
				@click="show = true" />
			<u-picker :closeOnClickOverlay="true" @close=" show = false" :show="show" @cancel="cancel" :columns="columns" @confirm="confirm" keyName="CITYNAME"></u-picker>
			<!-- <u-button @click="show = true">打开</u-button> -->
		</view>
		<view class="protocol_box">
			<view class="select" :class="{ active: agree }" @click="agree = !agree"></view>
			我已同意
			<text @click="onPageJump('/pages/user/protocol')">《用户协议》</text>
			和
			<text @click="onPageJump('/pages/user/protocol')">《隐私协议》</text>
		</view>
		<view class="btn_box"><button @click="onSubmit">绑定</button></view>
	</view>
</template>
<script>
import md5 from '@/plugins/md5';
import { getRegister } from '@/common/utils/api.js'
var clear;
export default {
	data() {
		return {
			//手机号
			phone: '',
			// 密码
			password: '',
			//验证码
			code: '',
			//确认密码
			confirmPassword: '',
			// city
			recommendCode: "",
			cityCode: "",
			//验证码
			codeText: '获取验证码',
			//验证码已发
			readonly: false,
			agree: false,
			show: false,
			columns: [
				[
					{
						"CITYCODE": 330100000000,
						"CITYNAME": "杭州市",
						"CITYID": 2,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 2
					},
					{
						"CITYCODE": 330200000000,
						"CITYNAME": "宁波市",
						"CITYID": 3,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 3
					},
					{
						"CITYCODE": 330300000000,
						"CITYNAME": "温州市",
						"CITYID": 4,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 4
					},
					{
						"CITYCODE": 330400000000,
						"CITYNAME": "嘉兴市",
						"CITYID": 5,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 5
					},
					{
						"CITYCODE": 330500000000,
						"CITYNAME": "湖州市",
						"CITYID": 6,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 6
					},
					{
						"CITYCODE": 330600000000,
						"CITYNAME": "绍兴市",
						"CITYID": 7,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 7
					},
					{
						"CITYCODE": 330700000000,
						"CITYNAME": "金华市",
						"CITYID": 8,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 8
					},
					{
						"CITYCODE": 330800000000,
						"CITYNAME": "衢州市",
						"CITYID": 9,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 9
					},
					{
						"CITYCODE": 330900000000,
						"CITYNAME": "舟山市",
						"CITYID": 10,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 10
					},
					{
						"CITYCODE": 331000000000,
						"CITYNAME": "台州市",
						"CITYID": 11,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 11
					},
					{
						"CITYCODE": 331100000000,
						"CITYNAME": "丽水市",
						"CITYID": 12,
						"PID": "330000000000",
						"CENTERX": null,
						"CENTERY": null,
						"DISPLAYORDER": 12
					}
				]
			],
		};
	},
	//第一次加载
	onLoad(e) {
		if (ZWJSBridge) {
			ZWJSBridge.setTitle({
				title: "注册",
			})
				.then((res) => { })
				.catch((err) => { });
		}
		if (uni.getStorageSync('ISWechat') == '1') {
			this.phone = this.plusXing(uni.getStorageSync('userData').personInfo.phone)
			this.password = this.plusXing(uni.getStorageSync('userData').personInfo.idNo)
		} else if (uni.getStorageSync('ISWechat') == '0' && (uni.getStorageSync('isPer') == '1' || uni.getStorageSync('isPer') == '0')) {
			this.phone = this.plusXing(uni.getStorageSync('userData').mobile)
			this.password = this.plusXing(uni.getStorageSync('userData').idnum)
		}
	},
	//页面显示
	onShow() { },
	//方法
	methods: {
		plusXing(str, frontLen, endLen) {
			//浙里办新规则
			if (str.length < 9) {
				return '*' + str.substring(1, str.length) //截取name 字符串截取第一个字符
			} else if (str.length > 9 && str.length < 18) {
				return str.substring(0, 3) + '****' + str.substring(7, str.length) //截取第一个和大于第4个字符
			} else {
				return str.substring(0, 1) + '****************' + str.substring(str.length - 1, str.length)
			}
		},
		onJumpPage(url) {
			uni.navigateTo({
				url: url
			});
		},
		confirm(e) {
			this.show = false
			this.recommendCode = e.value[0].CITYNAME
			this.cityCode = e.value[0].CITYCODE
		},
		cancel() {
			this.show = false
		},
		//获取验证码
		getCode() {
			if (this.readonly) {
				uni.showToast({
					title: '验证码已发送',
					icon: 'none'
				});
				return;
			}
			if (!this.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			if (!this.$base.phoneRegular.test(this.phone)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				});
				return;
			}
			this.$http
				.post('api/common/v1/send_sms', {
					phone: this.phone,
					type: 1000
				})
				.then(res => {
					this.getCodeState();
				});
		},
		//验证码按钮文字状态
		getCodeState() {
			const _this = this;
			this.readonly = true;
			this.codeText = '60S后重新获取';
			var s = 60;
			clear = setInterval(() => {
				s--;
				_this.codeText = s + 'S后重新获取';
				if (s <= 0) {
					clearInterval(clear);
					_this.codeText = '获取验证码';
					_this.readonly = false;
				}
			}, 1000);
		},
		async onSubmit() {
			if (!this.agree) {
				uni.showToast({
					title: '请先同意《用户协议》和《隐私协议》',
					icon: 'none'
				});
				return;
			}
			if (!this.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			if (!this.password) {
				uni.showToast({
					title: '请输入身份证号码',
					icon: 'none'
				});
				return;
			}
			if (!this.cityCode) {
				uni.showToast({
					title: '请选择地市',
					icon: 'none'
				});
				return;
			}
			if (uni.getStorageSync('ISWechat') == '1') {
				var httpData = {
					loginName: uni.getStorageSync('userData').personInfo.phone,
					cityCode: this.cityCode,
					idNum: uni.getStorageSync('userData').personInfo.idNo,
					phone: uni.getStorageSync('userData').personInfo.phone,
					roleType: "7",
					aliasName: uni.getStorageSync('userData').personInfo.userName,
					userId: uni.getStorageSync('userData').userId
				};
			} else if (uni.getStorageSync('ISWechat') == '0' && (uni.getStorageSync('isPer') == '1' || uni.getStorageSync('isPer') == '0')) {
				var httpData = {
					loginName: uni.getStorageSync('userData').mobile,
					cityCode: this.cityCode,
					idNum: uni.getStorageSync('userData').idnum,
					phone: uni.getStorageSync('userData').mobile,
					roleType: "7",
					aliasName: uni.getStorageSync('userData').username,
					userId: uni.getStorageSync('userData').userid
				};
			}
			const res = await getRegister(httpData)
			if (res.data.token) {
				uni.setStorageSync('token', res.data.token)
				uni.showToast({
					title: '注册成功',
					icon: 'success'
				});
				uni.navigateTo(
					{
						url: '/'
					}
				)
			} else {
				uni.showToast({
					title: '注册失败',
					icon: 'success'
				});
				uni.navigateTo(
					{
						url: '/pages/user/login'
					}
				)
			}
		}
	},
	//页面隐藏
	onHide() { },
	//页面卸载
	onUnload() { },
	//页面下来刷新
	onPullDownRefresh() { },
	//页面上拉触底
	onReachBottom() { },
	//用户点击分享
	onShareAppMessage(e) {
		return this.wxShare();
	}
};
</script>
<style lang="scss" scoped>
@import '@/style/mixin.scss';

.page {
	background-color: #FFF;
	padding: 0 65rpx;
	min-height: 100vh;

	.title {
		padding: 60rpx 0 40rpx 0;
		font-size: 60rpx;
		color: #333333;
	}

	.input_box {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		padding-top: 20rpx;
		border-bottom: 1rpx solid #eeeeee;

		input {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
		}

		button {
			height: 78rpx;
			line-height: 78rpx;
			font-size: 30rpx;
			color: $themeColor;

			&:active {
				background-color: transparent;
			}
		}
	}

	.btn_box {
		margin-top: 40rpx;

		button {
			font-size: 32rpx;
			@include theme('btn_bg');
			color: #fff;
			height: 100rpx;
			line-height: 100rpx;
			border-radius: 8rpx;
		}
	}

	.protocol_box {
		margin-top: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		font-size: 28rpx;
		color: #333333;

		.select {
			width: 36rpx;
			height: 36rpx;
			background-image: url("../../static/icon/ic_gender_unselected.png");
			background-position: center center;
			background-repeat: no-repeat;
			background-size: 100% auto;
			margin-right: 15rpx;

			&.active {
				background-image: url("../../static/icon/ic_agreed.png");
			}
		}

		>text {
			color: $themeColor;
		}
	}
}
</style>
